﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fashion.Common.Const;
using Fashion.Models;
using Fashion.Models.Interface;
using Fashion.Models.Model;

namespace Fashion.Controllers
{
    public class CategoryController : BaseController
    {
        private IProductCategoryRepository _repository;
        public CategoryController()
        {
            _repository = new ProductCategoryRepository();
        }

        #region Category
        
        [Authorize]
        public ActionResult List()
        {
            var model = _repository.GetAllCategoryParent().OrderBy(i=>i.OrderDisplay);
            return View(model);
        }

        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [Authorize]
        public ActionResult Create(ProductCategoryModel model)
        {
            if (!ModelState.IsValid)
                return View(model);
            ProductCategory category = new ProductCategory();
            category.Name = model.Name;
            category.OrderDisplay = model.OrderDisplay;
            _repository.AddNewCategory(category);
            return RedirectToAction("List");
        }

        [Authorize]
        public ActionResult Update(int id)
        {
            var category = _repository.GetCategoryById(id);
            var model = new ProductCategoryModel()
                            {
                                Id = category.Id,
                                Name = category.Name,
                                OrderDisplay = category.OrderDisplay,
                            };
            return View(model);
        }

        [HttpPost]
        [Authorize]
        public ActionResult Update(ProductCategoryModel model)
        {
            if (!ModelState.IsValid)
                return View(model);
            var category = _repository.GetCategoryById(model.Id);
            category.Name = model.Name;
            category.OrderDisplay = model.OrderDisplay;
            _repository.Update();
            return RedirectToAction("List");
        }

        [Authorize]
        public ActionResult Delete(int id)
        {
            _repository.DeleteCategory(id);
            return RedirectToAction("List");
        }

        public ActionResult Detail(int id)
        {
            var category = _repository.GetCategoryById(id);
            return View(category);
        }

        #endregion
    }
}
